﻿
Public Class Frm_SALRetailGiftUsed
    Implements IDServer.IDServer.IDServer

    Private MasterDS As DataSet


#Region "System Variabled Define"

    Public ActionHelp As IDServer.IDServer.ActionBtn = IDServer.IDServer.ActionBtn.ActionHelp

    Private WithEvents MainForm As New Frm_MainForm

    Public Property SelectObject() As Frm_MainForm
        Get
            Return MainForm
        End Get
        Set(ByVal value As Frm_MainForm)
            MainForm = value
        End Set
    End Property

#End Region

#Region "System Variabled Define"
    Public Function ActionAdd() As Boolean Implements IDServer.IDServer.IDServer.ActionAdd

    End Function

    Public Function ActionCancel() As Boolean Implements IDServer.IDServer.IDServer.ActionCancel

    End Function

    Public Function ActionDelete() As Boolean Implements IDServer.IDServer.IDServer.ActionDelete

    End Function

    Public Function ActionEdit() As Boolean Implements IDServer.IDServer.IDServer.ActionEdit

    End Function

    Public Function ActionExport() As Boolean Implements IDServer.IDServer.IDServer.ActionExport

    End Function

    Public Function ActionFilter() As Boolean Implements IDServer.IDServer.IDServer.ActionFilter

    End Function

    Public Function ActionGoFirst() As Boolean Implements IDServer.IDServer.IDServer.ActionGoFirst

    End Function

    Public Function ActionGoLast() As Boolean Implements IDServer.IDServer.IDServer.ActionGoLast

    End Function

    Public Function ActionGoNext() As Boolean Implements IDServer.IDServer.IDServer.ActionGoNext

    End Function

    Public Function ActionGoPrevious() As Boolean Implements IDServer.IDServer.IDServer.ActionGoPrevious

    End Function

    Public Function ActionImport() As Boolean Implements IDServer.IDServer.IDServer.ActionImport

    End Function

    Public Function ActionInitialize() As Boolean Implements IDServer.IDServer.IDServer.ActionInitialize

    End Function

    Public Function ActionPrint() As Boolean Implements IDServer.IDServer.IDServer.ActionPrint

    End Function

    Public Function ActionSave() As Boolean Implements IDServer.IDServer.IDServer.ActionSave

    End Function

    Public Function ActionSelect() As Boolean Implements IDServer.IDServer.IDServer.ActionSelect

    End Function

#End Region

    Private Sub UseShopNo()
        Try
            Dim Dset As DataSet
            Dim sql As String = ""
            sql = "select distinct FCname   from dbo.BSC_DEPT a right join BSC_GiftUsed b on a.Fcode = b.usedept "
            Dset = Exec(sql, E_ExecType.DataSet)
            Me.CBShopNo.DataSource = Dset.Tables(0)
            Me.CBShopNo.DisplayMember = "FCname"

        Catch ex As Exception
            MessageBox.Show("Initialize:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK)
        End Try

    End Sub

    Private Sub BindMasterGrid()
        Dim mTable As New DataTable()
        Try
            With mTable
                .Columns.Add("isSelected", GetType(Boolean))
                .Columns.Add("Num")
                .Columns.Add("CName")
                .Columns.Add("FaceAmount")
                .Columns.Add("Satus")
                .Columns.Add("UseDate")
                .Columns.Add("UseDept")
                .Columns.Add("UseBy")
                .Columns.Add("UseComputer")
                .Columns.Add("checkedby")

            End With

            MasterDS = New DataSet
            MasterDS.Tables.Add(mTable)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)

            With Me.DataGridView1
                For i As Integer = 0 To .Columns.Count - 1
                    Dim mKey As String = .Columns(i).Name
                    Dim CaptionStr As String = BindHashTable(1).Item(mKey)
                    .Columns(i).HeaderText = CaptionStr
                Next
                .AllowUserToAddRows = False
                .AllowUserToDeleteRows = False
                .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            End With

        Catch ex As Exception
            MsgBox("BindDetailGrid", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mTable = Nothing
        End Try
    End Sub

    Private Function BindHashTable(Optional ByVal Index As Integer = 0) As Hashtable
        Dim mHash As New Hashtable
        Try

            With mHash
                .Add("isSelected", "选择")
                .Add("Num", "禮券編號")
                .Add("CName", "禮券名稱")
                .Add("FaceAmount", "禮券面值")
                .Add("Satus", "狀態")
                .Add("UseDate", "使用日期")
                .Add("UseDept", "門店")
                .Add("UseBy", "收銀員")
                .Add("UseComputer", "使用電腦")
                .Add("checkedby", "核准者")

            End With
            Return mHash
        Catch ex As Exception
            Return Nothing
            MsgBox("BindHashTable" & ex.ToString, MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, Msg.CompanyName)
        Finally
            mHash = Nothing

        End Try
    End Function

    Private Sub BtnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnFind.Click
        Find()
    End Sub

    Private Sub Find()
        Try
            Me.Cursor = Cursors.WaitCursor
            Dim Sql As String = ""
            Dim WhereSql As String = Me.ReturnWhereSql()

            Sql = " select convert(bit,0) as isSelected,a.Num,b.CName,b.FaceAmount,Satus=CASE a.Satus WHEN '1' THEN '已使用' ELSE '核准'  END,UseDate,c.fCname as UseDept,UseBy,UseComputer,checkedby  " & vbCrLf
            Sql += "from BSC_GiftUsed a left join Bsc_Gift b on a.num = b.num left join BSC_DEPT c on a.usedept = c.Fcode " & vbCrLf
            Sql &= " " & WhereSql

            Dim MasterDS As New DataSet
            MasterDS = ReturnDataset(Sql)
            Me.DataGridView1.DataSource = MasterDS.Tables(0)
            BindDataGridViewCaption(Me.DataGridView1, Me.BindHashTable(0))
            FullFormatDataGridView(Me.DataGridView1)
            If MasterDS.Tables(0).Select.Length <= 0 Then
                MessageBox.Show("對不起，沒有查詢結果", "信息提示", MessageBoxButtons.OK, MessageBoxIcon.Information)
            End If
            Me.CheckBox1.Checked = False
            '' SetGridRowBackColor(Me.DataGridView1, MasterDS)

        Catch ex As Exception
            MessageBox.Show("ExecFind:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            Me.Cursor = Cursors.Default
        End Try

    End Sub

    ''' <summary>
    ''' Return Exec Find Condition
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    ''' 

    Private Function ReturnWhereSql() As String
        Dim Sql As String = ""
        Dim Status As String


        Try

            Sql = " Where 1=1 "
            If Me.TBGiftNo.Text.Trim <> "" Then
                Sql &= "  and  a.Num = '" & Replace_Text(Me.TBGiftNo.Text.Trim) & "'"
            End If

            If Me.TBGiftName.Text.Trim <> "" Then
                Sql &= "  and b.CName =  '" & Replace_Text(Me.TBGiftName.Text.Trim) & "'"
            End If

            If Me.CBStatus.Text.Trim <> "" Then
                If Me.CBStatus.Text = "已使用" Then
                    Status = "1"
                Else
                    Status = "核准"
                End If
                Sql &= "  and Isnull(Satus,'')= '" & Status & "'"
            End If

            If Me.CBShopNo.Text.Trim <> "" Then
                Sql &= "  and Isnull(c.FCname,'') = '" & Replace_Text(Me.CBShopNo.Text.Trim) & "'"
            End If

            If Me.CheckBox2.Checked = True Then
                Sql &= " and  '" & Format(Me.DateTimePicker1.Value, "yyyy-MM-dd") & "'  between b.startdate and b.enddate "
            End If

            If Me.CheckBox3.Checked = True Then
                Sql &= "  and  UseDate ='" & Format(Me.DateTimePicker2.Value, "yyyy-MM-dd") & "'"
            End If
        Catch ex As Exception
            MessageBox.Show("ReturnWhereSql:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
        Return Sql
    End Function

    Private Sub Frm_SALRetailGiftUsed_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        UseShopNo()
        BindMasterGrid()
    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox2.CheckedChanged
        If Me.CheckBox2.Checked = True Then
            Me.DateTimePicker1.Enabled = True
        Else
            Me.DateTimePicker1.Enabled = False
        End If
    End Sub

    Private Sub CheckBox3_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox3.CheckedChanged
        If Me.CheckBox3.Checked = True Then
            Me.DateTimePicker2.Enabled = True
        Else
            Me.DateTimePicker2.Enabled = False
        End If
    End Sub

    Private Sub CheckBox1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox1.CheckedChanged
        Dim mTable As DataTable = DirectCast(Me.DataGridView1.DataSource, DataTable)
        If Me.CheckBox1.Checked = True Then
            If mTable.Rows.Count > 0 Then
                For Each row As DataRow In mTable.Rows
                    row("isselected") = True
                Next
            End If
        End If
        If Me.CheckBox1.Checked = False Then
            If mTable.Rows.Count > 0 Then
                For Each row As DataRow In mTable.Rows
                    row("isselected") = False
                Next
            End If
        End If
    End Sub

    Private Sub Checked_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Checked.Click
        Try
            Dim Tb As DataTable = DirectCast(Me.DataGridView1.DataSource, DataTable)
            Tb.AcceptChanges()
            Dim Rows() As DataRow = Tb.Select("Isselected=True")
            If Rows.Length = 0 Then
                MsgBox("對不起，沒有禮券可以審核！", MsgBoxStyle.Information + MsgBoxStyle.OkOnly, Msg.CompanyName)
                Exit Sub
            End If
            Dim fNum As System.Text.StringBuilder = New System.Text.StringBuilder
            For Each row As DataRow In Rows
                If fNum.Length > 0 Then
                    fNum.Append(",")
                End If
                fNum.Append("'")
                fNum.Append(row.Item("Num").ToString)
                fNum.Append("'")
            Next

            Dim sql As String
            sql = "update bsc_giftused set Satus = '核准',checkedby = '" & PubUserName & "' where Num in (" & fNum.ToString & ");"

            For Each row As DataRow In Rows
                row.Item("Satus") = "核准"
                row.Item("checkedby") = PubUserName
            Next

            Tb.AcceptChanges()



            Dim Dtab As New DataTable
            Dtab = Exec(sql, E_ExecType.ExecuteNonQuery)

            For Each row As DataRow In Rows
                row.Item("Satus") = "核准"
                row.Item("checkedby") = PubUserName
            Next

            Tb.AcceptChanges()
            '' Find()
        Catch ex As Exception

        End Try
    End Sub

    Private Sub DataGridView1_RowPostPaint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewRowPostPaintEventArgs) Handles DataGridView1.RowPostPaint
        Try
            Dim DGView As DataGridView = DirectCast(sender, DataGridView)
            Dim SBrush As New SolidBrush(DGView.RowHeadersDefaultCellStyle.ForeColor)
            e.Graphics.DrawString(Convert.ToString(e.RowIndex + 1, System.Globalization.CultureInfo.CurrentCulture), e.InheritedRowStyle.Font, SBrush, e.RowBounds.Location.X + 20, e.RowBounds.Location.Y + 4)
        Catch ex As Exception
            MessageBox.Show("DataGridViewstorage_RowPostPaint:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

    Private Sub SetGridRowBackColor(ByVal Grid As DataGridView, ByVal DS As DataSet)
        Try
            If DS.Tables(0).Rows.Count <= 0 Then Exit Sub

            For i As Integer = 0 To DS.Tables(0).Rows.Count - 1
                If DS.Tables(0).Rows(i).Item("Satus").ToString = "已使用" Then
                    Grid.Rows(i).DefaultCellStyle.BackColor = Color.LightGray
                ElseIf DS.Tables(0).Rows(i).Item("Satus").ToString = "核准" Then
                    Grid.Rows(i).DefaultCellStyle.BackColor = Color.LightCyan

                End If
            Next

        Catch ex As Exception
            MsgBox("SetGridRowBackColor:" + ex.ToString, MsgBoxStyle.Exclamation + MsgBoxStyle.OkOnly, Msg.CompanyName)
        End Try
    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
        Try
            Dim mtable As DataTable = DirectCast(Me.DataGridView1.DataSource, DataTable)
            'Me.ExecFind("")
            If mtable.Rows.Count > 0 Then
                Me.DataGridView1.CurrentRow.Selected = True
                If Me.DataGridView1.CurrentRow.Cells(0).Value = False Then
                    Me.DataGridView1.CurrentRow.Cells(0).Value = True
                Else
                    Me.DataGridView1.CurrentRow.Cells(0).Value = False
                End If

            End If
        Catch ex As Exception
            MessageBox.Show("對不起，請先查詢此單！", Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
            'MessageBox.Show("DGV_Master_CellClick:" & ex.Message & ex.StackTrace, Msg.CompanyName, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub DataGridView1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles DataGridView1.Paint
        FormatDgrd(DataGridView1, "Satus")
    End Sub
End Class
